<?php
if(!defined('SYSTEM_PATH' ) ) die('Cannot access this page directly or see your configuration file ' ) ;

/**
 * GlobalController
 * 
 * @desc 		Global controller that's help programming complex application 
 * @package 	Controller
 * @see		Controller
 * 
 * @author 	menTiss
 * 
 * @copyright 	Copyright (c) 2007 
 * @date 		01/11/2007
 * @version 	1.0.1
 * @since 	1.0.0 - 14/11/2007 : 	Fixed bug to access direct method of global controller in constructor 
 * 								Added comments to _load method
 * 								Renamed method isController to isSubController
 * 
 * @access 	public
 */
class GlobalController extends Controller {
	protected $_controller = null ;
	protected $_name = null ;
	
	/**
	 * GlobalController::_load()
	 * 
	 * @desc 		Execute method or other controller->method
	 * 			If you want to use data in global controller method context of method must be like <method>__<data>
	 * 			If you to execute a method to a sub controller please use this syntax for method's context <sub-controller>__<sub-controller_method>
	 * @warning 	You must not name a global controller method like a sub controller name, it will give must of the time a non expected result.
	 * 			Do not use index has sub controller name !
	 * 
	 * @author 	menTiss
	 * 
	 * @date 		01/11/2007
	 * 
	 * @return 	bool
	 * 
	 * @access 	public
	 */
	public function _map() {
		$launched = false ;
		if(is_null($this->_name)) {
			$this->_name = strtolower(get_class($this)) ;
			$this->template->setPath('all',strtolower($this->_name)) ;
		}		
		if(!is_null($this->_core->getContext('method')) && !ereg('__',$_GET['m'])) {
			$this->template->assign('controller', $this->_name.'_'.strtolower($this->_core->getContext('method'))) ;
			$path = SITE_PATH.'controllers/'.$this->_name.'/'.$class.EXT ;
			if(file_exists($path)) {
				$this->_core->setContext('method', (is_null($this->_core->getContext('data'))) ? '' : $this->_core->getContext('data')) ;
				$this->_core->setContext('data', (is_null($this->_core->getContext('param'))) ? '' : $this->_core->getContext('param')) ;
				$this->_controller =& $load->controller($this->_name.'/'.$class) ;
			}			
			if(!($this->_controller instanceof Controller)) {
				$this->_controller = null ;
			} else {
				$launched = true ;
			}
		}
		
		if(is_null($this->_controller)) {
			if(!is_null($this->_core->getContext('method')) && ereg('__', $this->_core->getContext('method'))) {
				$method = substr($this->_core->getContext('method'), 2) ;
				if(method_exists($this, $method)) {
					$this->template->assign('method', $method) ;
					$launched = true ;
					if(!is_null($this->_core->getContext('data'))) 
						$this->$method($this->_core->getContext('data')) ;
					else 
						$this->$method() ;					
				}
			} elseif(!is_null($this->_core->getContext('method'))) {
				if(method_exists($this, $this->_core->getContext('method'))) {
					$this->template->assign('method',$this->_core->getContext('method')) ;
					$this->{$this->_core->getContext('method')}() ;
					$launched = true ;
				}
			}
			
			if(!$launched && method_exists($this, 'index')) {
				$this->_core->setContext('method', 'index') ;
				$launched = true ;
				$this->index() ;					
			} else {
				$this->sendError('The method ('.$this->_core->getContext('method').') you requested does not exists') ;
			}
		}
		return $launched;
	}
	/**
	 * GlobalController::isSubController()
	 * 
	 * @desc 		Is a sub-controller is set ? 
	 * 
	 * @author 	menTiss
	 * 
	 * @date 		01/11/2007
	 * 
	 * @return 	bool
	 */
	public function isSubController() {
		return is_null($this->_controller);
	}
	/**
	 * GlobalController::setName()
	 * 
	 * @desc		Set global controller name and default template dir
	 * 
	 * @author 	menTiss
	 * @date 		14/11/2007
	 * 
	 * @param 	string $name
	 * 
	 * @return 	bool
	 */
	public function setName($name) {
		if(is_null($this->_name)) {
			$this->_name = strtolower($name) ;
			$this->template->setPath('all',strtolower($name).'/') ;
			return true;
		}
		return false;
	}
}
?>